package com.superteam.zzl.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.DynamicfragmentVpBinding;
import com.superteam.zzl.custom.DividerItemDecoration;
import com.superteam.zzl.model.DynamicFragment.bean.Dynamic;
import com.superteam.zzl.viewmodel.DynamicFragment.DynamicPresenter;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/7.
 */

public class DynamicFragmentVpAdapter extends BasePagerAdapter<Dynamic> {

    private DynamicPresenter presenter;
    private String[] titles;
    private DynamicfragmentVpBinding db;
    private List<Integer> convertPos;

    public DynamicFragmentVpAdapter(Context context, List<Dynamic> list, String[] titles, DynamicPresenter presenter) {
        super(context, list);
        this.titles = titles;
        this.presenter = presenter;
        convertPos = new LinkedList<>();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        Log.e("DynamicFragmentVpAdapter", "instantiateItem: " + list.size());
        if (convertPos.size() == 0) {
            db = DataBindingUtil.inflate(inflater, R.layout.dynamicfragment_vp, container, false);
            DynamicFragmentVpAllAdapter adapter = new DynamicFragmentVpAllAdapter(context, list.get(position).data.trends_list, presenter);
            adapter.setOnItemClickLitener(presenter);
            db.rc.setLayoutManager(new LinearLayoutManager(context));
            db.rc.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
            db.rc.setAdapter(adapter);
            datas[position] = db;
        } else {
            db = (DynamicfragmentVpBinding) datas[convertPos.get(0)];
        }
        container.addView(db.getRoot());
        return db.getRoot();
    }

    @Override
    public void notifyDataSetChanged() {
        datas = new ViewDataBinding[list.size()];
        super.notifyDataSetChanged();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(datas[position].getRoot());
//        convertPos.add(position);
    }



    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
